<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="referralCode">
              <el-input v-model="filters.referralCode" clearable placeholder="挂号编号"></el-input>
            </el-form-item>
            <!--<el-form-item prop="itemName">
              <el-input v-model="filters.itemName" placeholder="挂号项目"></el-input>
            </el-form-item>-->
            <el-form-item prop="patientName">
              <el-input v-model="filters.patientName" clearable placeholder="患者姓名"></el-input>
            </el-form-item>
            <el-form-item prop="referralState">
              <el-select v-model="filters.referralState" :clearable="true" placeholder="挂号状态">
                <el-option v-for="item in referralStatue" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <!--<el-form-item prop="hospitalId" v-if="userRole!=2">
              <el-select v-model="filters.hospitalId" :clearable="true" filterable :filter-method="listHospital" placeholder="请选择医院">
                <el-option
                  v-for="item in hospitalList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="date">
              <el-date-picker v-model="filters.date" :clearable="true" type="datetimerange" range-separator="至" start-placeholder="送检开始日期" end-placeholder="送检结束日期"></el-date-picker>
            </el-form-item>-->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>
    <!--<el-row>
      <div style="margin-left: 10px;margin-bottom: 10px;margin-top: 10px;" >
        <span>勾选批量操作：</span><el-button size="small" type="success" v-has="permissionBatchPrint" @click="handleClickBatchPrint" >批量打印</el-button>
      </div>
    </el-row>-->

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column prop="referralCode" label="编号"></el-table-column>
        <el-table-column prop="hospitalName" label="医院" width="150"></el-table-column>
        <el-table-column prop="institutionName" label="挂号医院" width="150"></el-table-column>
        <el-table-column width="120px" align="center" label="科室-医生">
          <template slot-scope="scope">
            <span>{{ scope.row.insOfficeName }} - {{ scope.row.doctorName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="referralDate" :formatter="formatDate" label="挂号日期"></el-table-column>
        <!--<el-table-column prop="referralTime" label="时间段"></el-table-column>-->
        <el-table-column width="120px" align="center" label="姓名-性别-年龄">
          <template slot-scope="scope">
            <span>{{ scope.row.patientName }} - {{ formatSex(scope.row.patientSex) }} - {{ scope.row.patientAge }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="patientCard" label="身份证"></el-table-column>
        <el-table-column prop="patientPhone" label="手机" width="100"></el-table-column>
        <el-table-column prop="referralEndTime" label="完成时间"></el-table-column>
        <el-table-column label="挂号状态" align="center" width="100">
          <!-- prop="" -->
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" :type="formatReferralStateType(scope.row.referralState)" effect="dark">
                {{ formatReferralState(scope.row.referralState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间"></el-table-column>
        <!--<el-table-column prop="remark" label="挂号备注"></el-table-column>-->
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <!--1.挂号中 10.协商中 20.挂号成功 30.已取消 40.待确认取消 50.待确认修改 60.已就诊-->
                <el-dropdown-item v-has="permissionEdit" v-if="scope.row.referralState === 1 || scope.row.referralState === 10" @click.native="handleClickEditor(scope.row)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item v-has="permissionAgreeUpdate" v-if="scope.row.referralState === 50 && userRole === 2" @click.native="handleClickAgreeUpdate(scope.row, 50)"
                  >同意修改</el-dropdown-item
                >
                <el-dropdown-item
                  v-has="permissionSuccess"
                  v-if="(scope.row.referralState === 1 || scope.row.referralState === 10) && userRole != 2"
                  @click.native="handleClickSuccess(scope.row, 20)"
                  >通过</el-dropdown-item
                >
                <el-dropdown-item
                  v-has="permissionCancel"
                  v-if="(scope.row.referralState === 1 || scope.row.referralState === 10) && userRole != 3"
                  @click.native="handleClickCancel(scope.row, 30)"
                  >取消</el-dropdown-item
                >
                <el-dropdown-item v-has="permissionAgreeCancel" v-if="scope.row.referralState === 40 && userRole === 3" @click.native="handleClickAgreeCancel(scope.row, 40)"
                  >同意取消</el-dropdown-item
                >
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.referralState === 30" @click.native="handleIsDisable(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
              <!--<el-button v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-button>-->
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import ReferralApi from '@/api/referral/referral';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import { getStorage } from '@/utils/auth';
import * as stn from '@/constants/storeName';

export default {
  name: 'ReferralManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      referralDataList: [],
      selectIds: [],
      userInfo: {},
      userRole: '',
      filters: {
        referralCode: '',
        patientName: '',
        referralState: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      hospitalList: [],
      //1.挂号中 10.协商中 20.挂号成功 30.已取消 40.待确认取消 50.待确认修改 60.已就诊
      referralStatue: [
        { label: '挂号中', value: 1 },
        { label: '协商中', value: 10 },
        { label: '挂号成功', value: 20 },
        { label: '已取消', value: 30 },
        { label: '待确认取消', value: 40 },
        { label: '待确认修改', value: 50 },
        { label: '已就诊', value: 60 },
      ],

      permissionAdd: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_EDIT',
      permissionSuccess: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_SUCCESS',
      permissionAgreeCancel: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_AGREE_CANCEL',
      permissionAgreeUpdate: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_AGREE_UPDATE',
      permissionCancel: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_CANCEL',
      permissionDelete: 'SYSTEM_MANAGER_REFERRAL_INFO_LIST_DELETE',
      excelData: {},
      allList: [],
    };
  },
  mounted() {
    window.addEventListener('keydown', this.keyDown);
    this.init();
    // 为了实现手机端下拉框可以打字搜索
    this.$Utils.removeSelectReadOnly();
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listReferral();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      this.userInfo = getStorage(stn.USER_INFO);
      if (this.userInfo) {
        this.userRole = this.userInfo.userRole;
      }
      _this.initDate();
      _this.listReferral();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    initDate() {
      let begin = new Date();
      begin.setHours(0);
      begin.setMinutes(0);
      begin.setSeconds(0);
      begin.setMonth(begin.getMonth() - 1);
      let end = new Date();
      end.setHours(23);
      end.setMinutes(59);
      end.setSeconds(59);
      this.filters.date = [begin, end];
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    formatSex(cellValue) {
      if (cellValue === 1) {
        return '男';
      } else if (cellValue === 2) {
        return '女';
      }
    },
    //1.挂号中 10.协商中 20.挂号成功 30.已取消 40.待确认取消 50.待确认修改 60.已就诊
    formatReferralState(cellValue) {
      if (cellValue === 1) {
        return '挂号中';
      } else if (cellValue === 10) {
        return '协商中';
      } else if (cellValue === 20) {
        return '挂号成功';
      } else if (cellValue === 30) {
        return '已取消';
      } else if (cellValue === 40) {
        return '待确认取消';
      } else if (cellValue === 50) {
        return '待确认修改';
      } else if (cellValue === 60) {
        return '已就诊';
      }
    },
    formatReferralStateType(cellValue) {
      if (cellValue === 1) {
        return '';
      } else if (cellValue === 10) {
        return '';
      } else if (cellValue === 20) {
        return 'success';
      } else if (cellValue === 30) {
        return 'warning';
      } else if (cellValue === 40) {
        return 'warning';
      } else if (cellValue === 50) {
        return 'info';
      } else if (cellValue === 60) {
        return 'success';
      }
    },
    formatState(row, column, cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      } else if (cellValue == 2) {
        return '待审核';
      }
    },
    formatDate(row, column, cellValue) {
      // 只返回日期 不返回时间
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listReferral();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listReferral();
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/referral/referralAdd',
        query: { id: row.referralId },
      });
    },
    handleClickAgreeCancel(row, state) {
      let _this = this;
      this.$confirm('是否同意医院取消该挂号', message.TITLE_PROMPT, {}).then(() => {
        ReferralApi.updateReferralState({
          id: row.referralId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listReferral();
        });
      });
    },
    handleClickAgreeUpdate(row, state) {
      let _this = this;
      this.$confirm('是否同意医检机构修改该挂号', message.TITLE_PROMPT, {}).then(() => {
        ReferralApi.updateReferralState({
          id: row.referralId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listReferral();
        });
      });
    },
    handleClickSuccess(row, state) {
      let _this = this;
      this.$confirm('是否通过该挂号申请', message.TITLE_PROMPT, {}).then(() => {
        ReferralApi.updateReferralState({
          id: row.referralId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listReferral();
        });
      });
    },
    handleClickCancel(row, state) {
      let _this = this;
      this.$confirm('是否取消该挂号申请', message.TITLE_PROMPT, {}).then(() => {
        ReferralApi.updateReferralState({
          id: row.referralId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listReferral();
        });
      });
    },

    handleIsDisable(row) {
      let _this = this;
      this.$confirm('是否删除该挂号信息', message.TITLE_PROMPT, {}).then(() => {
        ReferralApi.deleteReferral({
          referralId: row.referralId,
          state: -1,
        }).then((res) => {
          _this.$Utils.notifySuccess();
          _this.listReferral();
        });
      });
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/referral/referralAdd');
    },
    handleClickDetail(row) {
      // 查看
      let _this = this;
      _this.$router.push({
        path: '/referral/referralDetail',
        query: { id: row.referralId },
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listReferral();
    },
    listReferral() {
      // 过滤器
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        referralCode: _this.filters.referralCode,
        patientName: _this.filters.patientName,
        referralState: _this.filters.referralState,
      };
      ReferralApi.listReferral(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
